package io.lckgroup.modules.lck.controller.wxmp;

import io.lckgroup.common.utils.R;
import io.lckgroup.modules.lck.annotation.WxLogin;
import io.lckgroup.modules.lck.annotation.WxLoginUser;
import io.lckgroup.modules.lck.entity.CustBaseEntity;
import io.lckgroup.modules.lck.entity.CustomerInfoEntity;
import io.lckgroup.modules.lck.entity.qo.CustBaseQo;
import io.lckgroup.modules.lck.entity.qo.updateCustBaseQo;
import io.lckgroup.modules.lck.service.CustBaseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import oracle.jdbc.proxy.annotation.Post;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author huangrui
 */
@RestController
@RequestMapping("/mp/user/base")
@Api(value="用户基础信息controller",tags={"用户操作接口"})
public class CustBaseController {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private CustBaseService custBaseService;

    @GetMapping("/getUserBase")
    @ApiOperation(value = "根据用户id查询用户补充资料")
    @WxLogin
    public R getUserBase(@WxLoginUser CustomerInfoEntity customerInfoEntity) {
        try {
            logger.info("查询用户补录信息");
            List<CustBaseEntity> customerByUserId = custBaseService.getCustomerByUserId(customerInfoEntity.getId().toString());
            return R.ok().put("custBase", customerByUserId);
        } catch (Exception e) {
            logger.error("查询失败");
            return R.error("查询失败");
        }
    }

    @GetMapping("/getChild")
    @ApiOperation(value = "根据用户id查询用户补充资料(child)")
    @WxLogin
    public R getChild(String id) {
        System.out.println(id);
        try {
            logger.info("查询小朋友信息");
            CustBaseEntity customerByUserId = custBaseService.getChild(id);
            return R.ok().put("data", customerByUserId);
        } catch (Exception e) {
            logger.error("查询失败");
            return R.error("查询失败");
        }
    }


    @PostMapping("/update")
    @ApiOperation(value = "根据用户id更新资料(child)")
    @WxLogin
    public R updateChild(@RequestBody updateCustBaseQo qo) {
        logger.info("入参",qo);
        try {
            logger.info("更新小朋友信息");
            boolean b = custBaseService.updateChild(qo);
            return R.ok().put("data", b);
        } catch (Exception e) {
            logger.error("更新失败");
            return R.error("更新失败");
        }
    }

    @PostMapping("/insert")
    @ApiOperation(value = "根据用户id新增资料(child)")
    @WxLogin
    public R insertChild(@RequestBody updateCustBaseQo qo,@WxLoginUser CustomerInfoEntity customerInfoEntity) {
        qo.setRole("2");
        qo.setUserId(customerInfoEntity.getId());
        System.out.println(customerInfoEntity.getId()+"-"+qo.getUserId());
        System.out.println("用户"+customerInfoEntity);
        logger.info("入参"+qo.toString());
        try {
            logger.info("新增小朋友信息");
            boolean b = custBaseService.insertChild(qo);
            return R.ok().put("data", b);
        } catch (Exception e) {
            logger.error("更新失败");
            return R.error("更新失败");
        }
    }

    @PostMapping("/delete")
    @ApiOperation(value = "根据用户id删除资料(child)")
    @WxLogin
    public R deleteChild(@RequestBody String id) {
        logger.info("入参"+id);
        try {
            logger.info("删除小朋友信息");
            boolean b = custBaseService.deleteChild(id);
            return R.ok().put("data", b);
        } catch (Exception e) {
            logger.error("删除失败");
            return R.error("删除失败");
        }
    }



    @WxLogin
    @PostMapping("/saveUserBase")
    @ApiOperation("保存客户信息")
    public R saveUserBase(@WxLoginUser CustomerInfoEntity customerInfoEntity,@RequestBody CustBaseQo custBaseQo){
        boolean b = custBaseService.saveUserBase(customerInfoEntity, custBaseQo);
        if(b){
            return R.ok();
        }
        return R.error();
    }



}
